package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqupt.qualityevaluation.dao.SchooltypecodeMapper;
import com.cqupt.qualityevaluation.pojo.schoollocationtypecode;
import com.cqupt.qualityevaluation.pojo.schooltypecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/schooltype")
public class SchoolTypeController {
    @Autowired
    private SchooltypecodeMapper schooltypecodeMapper;
    @GetMapping("/findAll")
    public List<schooltypecode> findAll(){
        return schooltypecodeMapper.selectList(null);
    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody schooltypecode s){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=schooltypecodeMapper.insert(s);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该学校类型代码已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","添加成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @DeleteMapping("/delete/{schooltypecode}")
    public String delete(@PathVariable("schooltypecode") String schooltypecode){
        QueryWrapper<schooltypecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schooltypecode",schooltypecode);
        schooltypecodeMapper.delete(queryWrapper);
        return "success";
    }
    @PutMapping("/update")
    public Object update(@RequestBody schooltypecode st){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<schooltypecode> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",st.getId());
        schooltypecode schooltypecode = new schooltypecode();
        schooltypecode.setSchooltypecode(st.getSchooltypecode());
        schooltypecode.setSchooltypename(st.getSchooltypename());
        try{
            result=schooltypecodeMapper.update(schooltypecode,updateWrapper);
        }
        catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该学校类型代码已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @GetMapping("/selectone/{id}")
    public schooltypecode findBySchoolType(@PathVariable("id") String id){
        QueryWrapper<schooltypecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return schooltypecodeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{schooltypecode}")
    public List<schooltypecode> findBySchoolTypeCode(@PathVariable("schooltypecode") String schooltypecode){
        QueryWrapper<schooltypecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schooltypecode",schooltypecode);
        return schooltypecodeMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{schooltypename}")
    public List<schooltypecode> findBySchoolTypeName(@PathVariable("schooltypename") String schooltypename){
        QueryWrapper<schooltypecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schooltypename",schooltypename);
        return schooltypecodeMapper.selectList(queryWrapper);
    }
}
